package com.zmn.plat.admin.controller.notice;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.stream.Collectors;

import com.zmn.common.constant.GlobalConsts;
import com.zmn.common.dictionary.GlobalDict;
import com.zmn.common.dto2.ResponseDTO;
import com.zmn.common.utils.number.NumberUtil;
import com.zmn.common.utils.reflect.BeanMapper;
import com.zmn.mcc.cas.model.LoginStaff;
import com.zmn.mcc.common.dto.dept.DeptDRO;
import com.zmn.plat.model.vo.notice.NoticeVO;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;
import com.zmn.plat.model.entity.notice.Notice;
import com.zmn.plat.model.entity.notice.NoticeQuery;

/**
 * 公告管理
 *
 * @author linfeng
 *
 * @since 2020/12/07 10:44
 */
@Controller
@RequestMapping("/notice/")
public class NoticeListController extends NoticeBaseController {

    /**
     * 列表
     * @author linfeng
     * @param query
     * @since 2020/12/15 11:01
     */
    @RequestMapping("list")
    @RequiresPermissions("/notice/list.action")
    public ModelAndView list(@ModelAttribute NoticeQuery query){

        List<Notice> data = noticeService.listPageByQuery(query);
        List<NoticeVO> items = BeanMapper.mapList(data, NoticeVO.class);

        List<Integer> deptOneIds = items.stream().map(e -> e.getDeptOneId()).distinct().collect(Collectors.toList());
        List<Integer> deptTwoIds = items.stream().map(e -> e.getDeptTwoId()).distinct().collect(Collectors.toList());
        List<Integer> deptIds = items.stream().map(e -> e.getDeptId()).distinct().collect(Collectors.toList());

        List<Integer> deptAllIds = new ArrayList<>();
        deptAllIds.addAll(deptOneIds);
        deptAllIds.addAll(deptTwoIds);
        deptAllIds.addAll(deptIds);
        deptAllIds = deptAllIds.stream().distinct().collect(Collectors.toList());

        List<DeptDRO> deptDROS = new ArrayList<>();
        ResponseDTO<List<DeptDRO>> responseDTO = deptListRemoteService.listDeptByDeptIds(deptAllIds);
        if (responseDTO.isSuccess()) {
            deptDROS = responseDTO.getData();
        }
        Map<Integer, String> deptNameMap = deptDROS.stream().collect(Collectors.toMap(e -> e.getDeptId(), e -> e.getDeptName()));

        items.forEach(e -> {
            if (Objects.equals(e.getDeptOneId(), e.getDeptId())) {
                e.setDeptUnionName(deptNameMap.get(e.getDeptId()));
            } else if (Objects.equals(e.getDeptTwoId(), e.getDeptId())) {
                e.setDeptUnionName(deptNameMap.get(e.getDeptOneId()) + "-" + deptNameMap.get(e.getDeptTwoId()));
            } else {
                e.setDeptUnionName(deptNameMap.get(e.getDeptOneId()) + "-" + deptNameMap.get(e.getDeptTwoId()) + "-" + deptNameMap.get(e.getDeptId()));
            }
        });

        ModelAndView mav = new ModelAndView("notice/list");
        mav.addObject("items", items);
        mav.addObject("vo", query);
        return mav;
    }


    /**
     * 详情
     * @author linfeng
     * @param relateId
     * @since 2020/12/15 11:01
     */
    @RequestMapping("detail")
    @RequiresPermissions("/notice/detail.action")
    public ModelAndView detail(@RequestParam(value = "relateId", defaultValue = "0") Integer relateId){
        Notice notice;
        if (NumberUtil.isNullOrZero(relateId)) {
            notice = new Notice();
        } else {
            notice = noticeService.findByKey(relateId);
        }

        ModelAndView mav = new ModelAndView("notice/detail");
        mav.addObject("notice", notice);

        mav.addObject("orgOnes", organizeListRemoteService.listByParentId(GlobalConsts.TOP_ID, GlobalDict.STATUS_NULL).getData());
        mav.addObject("orgTwos", !NumberUtil.isNullOrZero(notice.getPlat()) ? organizeListRemoteService.listByParentId(notice.getPlat().intValue()).getData() : new ArrayList<>());
        mav.addObject("orgThrs", !NumberUtil.isNullOrZero(notice.getOrgTwoId()) ? organizeListRemoteService.listByParentId(notice.getPlat().intValue()).getData() : new ArrayList<>());

        mav.addObject("deptOnes", deptListRemoteService.listDeptByParentId(GlobalConsts.TOP_ID, GlobalDict.STATUS_NULL).getData());
        mav.addObject("deptTwos", !NumberUtil.isNullOrZero(notice.getDeptOneId()) ? deptListRemoteService.listDeptByParentId(notice.getDeptOneId(), GlobalDict.STATUS_NULL).getData() : new ArrayList<>());
        mav.addObject("deptThrs", !NumberUtil.isNullOrZero(notice.getDeptTwoId()) ? deptListRemoteService.listDeptByParentId(notice.getDeptTwoId(), GlobalDict.STATUS_NULL).getData() : new ArrayList<>());

        return mav;
    }

    @RequestMapping("getStaffNotice")
    @ResponseBody
    public ResponseDTO getStaffNotice() {
        LoginStaff staff = getMcStaff();
        Notice notice = noticeBService.getStaffNotice(staff.getDeptId());
        return ResponseDTO.success(notice);
    }
}
